package cn.iocoder.yudao.module.cms.appversion.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.cms.appversion.dal.dataobject.AppVersionDO;
import cn.iocoder.yudao.module.cms.appversion.service.AppVersionService;
import cn.iocoder.yudao.module.cms.appversion.vo.AppVersionPageReqVO;
import cn.iocoder.yudao.module.cms.appversion.vo.AppVersionRespVO;
import cn.iocoder.yudao.module.cms.appversion.vo.AppVersionSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 版本更新")
@RestController
@RequestMapping("/cms/app-version")
@Validated
public class AppVersionController {

    @Resource
    private AppVersionService appVersionService;

    @PostMapping("/create")
    @Operation(summary = "创建版本更新")
    @PreAuthorize("@ss.hasPermission('cms:app-version:create')")
    public CommonResult<Long> createAppVersion(@Valid @RequestBody AppVersionSaveReqVO createReqVO) {
        return success(appVersionService.createAppVersion(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新版本更新")
    @PreAuthorize("@ss.hasPermission('cms:app-version:update')")
    public CommonResult<Boolean> updateAppVersion(@Valid @RequestBody AppVersionSaveReqVO updateReqVO) {
        appVersionService.updateAppVersion(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除版本更新")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:app-version:delete')")
    public CommonResult<Boolean> deleteAppVersion(@RequestParam("id") Long id) {
        appVersionService.deleteAppVersion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得版本更新")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:app-version:query')")
    public CommonResult<AppVersionRespVO> getAppVersion(@RequestParam("id") Long id) {
        AppVersionDO appVersion = appVersionService.getAppVersion(id);
        return success(BeanUtils.toBean(appVersion, AppVersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得版本更新分页")
    @PreAuthorize("@ss.hasPermission('cms:app-version:query')")
    public CommonResult<PageResult<AppVersionRespVO>> getAppVersionPage(@Valid AppVersionPageReqVO pageReqVO) {
        PageResult<AppVersionDO> pageResult = appVersionService.getAppVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppVersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出版本更新 Excel")
    @PreAuthorize("@ss.hasPermission('cms:app-version:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppVersionExcel(@Valid AppVersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppVersionDO> list = appVersionService.getAppVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "版本更新.xls", "数据", AppVersionRespVO.class,
                        BeanUtils.toBean(list, AppVersionRespVO.class));
    }

}
